<?php
class SitelutionsDNS
{
  private $_user=null;
  private $_pass=null;
  private $_client=null;
  
  
  function SitelutionsDNS($user, $password, $location=null, $uri=null){
    if ($location == null) $location='https://api.sitelutions.com/soap-api';
    if ($ur == null) $uri='https://api.sitelutions.com/API';
    $this->_user=new SoapParam($user, 'user');
    $this->_pass=new SoapParam($password, 'password');
    $this->_client=new SoapClient(null, array('uri'=>$uri, 'location'=>$location));
  }

  function listDomains(){
    $domainList=$this->getArrTable($this->call('listDomains', array()));
    return $domainList;
  }
  
  function addRR($domainid, $data, $type, $hostname, $ttl=null, $aux=null){
    $prm=array('domainid'=>$domainid, 'data'=>$data, 'type'=>$type, 'hostname'=>$hostname);
    if ($ttl != null) $prm['ttl']=$ttl;
    if ($aux != null) $prm['aux']=$aux;
    
    $rr_id=$this->call('addRR', $prm);
    return $rr_id;
  }

  function getDomainByName($name){
    $domain=$this->call('getDomainByName', array('name'=>$name));
    return $this->getObjArr($domain);
  }  

  function getDomainByID($domainid){
    $domain=$this->call('getDomainByID', array('domainid'=>$domainid));
    return $this->getObjArr($domain);    
  }
  

  function listRRsByDomain ($domainid){
    $domainList=$this->getArrTable($this->call('listRRsByDomain', array('domainid'=>$domainid)));
    return $domainList;
    return $rrList;
  }

  
  function getRRByID($rrid){
    $rr=$this->call('getRRByID', array('rrid'=>$rrid));
    return $this->getObjArr($rr);    
  }

  function updateDomain ($domainid, $alertexpire=null, $autorenew=null, $mbox=null, $expire=null, $minimum=null, $ns=null, $serial=null, $retry=null, $refresh=null, $ttl=null){
    $prm['domainid']=$domainid;
    if ($alertexpire != null) $prm['alertexpire']=($alertexpire==true) ? 1:0;
    if ($autorenew != null) $prm['autorenew']=($autorenew==true) ? 1:0;
    if ($mbox != null) $prm['mbox']=$mbox;
    if ($expire != null) $prm['expire']=$expire;
    if ($minimum != null) $prm['minimum']=$minimum;
    if ($ns != null) $prm['ns']=$ns;
    if ($serial != null) $prm['serial']=$serial;
    if ($retry != null) $prm['retry']=$retry;
    if ($refresh != null) $prm['refresh']=$refresh;
    if ($ttl != null) $prm['ttl']=$ttl;
    $this->call('updateDomain',$prm);
    return true;
  }


  function addDomain ($name, $alertexpire=null, $autorenew=null, $mbox=null, $expire=null, $minimum=null, $ns=null, $serial=null, $retry=null, $refresh=null, $ttl=null, $no_default_ns=null){
    $prm['name']=$name;
    if ($alertexpire != null) $prm['alertexpire']=($alertexpire==true) ? 1:0;
    if ($autorenew != null) $prm['autorenew']=($autorenew==true) ? 1:0;
    if ($mbox != null) $prm['mbox']=$mbox;
    if ($expire != null) $prm['expire']=$expire;
    if ($minimum != null) $prm['minimum']=$minimum;
    if ($ns != null) $prm['ns']=$ns;
    if ($serial != null) $prm['serial']=$serial;
    if ($retry != null) $prm['retry']=$retry;
    if ($refresh != null) $prm['refresh']=$refresh;
    if ($ttl != null) $prm['ttl']=$ttl;
    $domainid=$this->call('addDomain', $prm);
    return $domainid;
  }

  function deleteRR($rrid){
    $this->call('deleteRR', array('rrid'=>$rrid));
    return true;
  }

  function deleteDomain($domainid, $confirmation){
    $prm=array('domainid'=>$domainid);
    if ($confirmation) $prm['i_know_all_RRs_for_this_domain_will_be_deleted'] = 1;
    $this->call('deleteDomain', $prm);
    return true;
  }

  function updateRR($rrid, $ttl=null, $data=null, $aux=null){
    $prm['rrid']=$rrid;
    if ($ttl != null) $prm['ttl']=$ttl;
    if ($aux != null) $prm['aux']=$aux;
    if ($data != null) $prm['data']=$data;
    $this->call('updateRR', $prm);
    return true;
  }
  
  private function call($function, $parameters){
    $client=$this->_client;
    $prm=array($this->_user, $this->_pass);
    foreach($parameters AS $name=>$value){
      $prm[]=new SoapParam($value, $name);
    }
    $r = $client->__soapCall($function, $prm);
    return $r;
  }
  
  private function getArrTable($objects){
    foreach ($objects AS $me){
      $x[]=(array) $me;
    }
    return $x;
  }
  
  private function getObjArr($object){
    return (array)$object;
  }

}
?>